/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：FunNodeDaoImpl.java
 * History:
 *       2012-1-13: Initially created, tangkf.
 */
package com.xteam.purview.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.tangkf.utils.CommonUtils;
import com.xteam.db.IBaseDao;
import com.xteam.purview.entities.PurviewOrg;
import com.xteam.purview.entities.PurviewUser;
import com.xteam.purview.entities.PurviewUserOrg;
import com.xteam.purview.service.AbsFace;
import com.xteam.purview.service.IPurviewOrgFace;

/**
 * 组织机构接口
 * @author tangkf ---2012-1-13
 */
public class PurviewOrgFace extends AbsFace<PurviewOrg> implements IPurviewOrgFace {
	
	private PurviewOrgFace() {
	}

	private static PurviewOrgFace	instance	= new PurviewOrgFace();

	public static PurviewOrgFace getInstance() {
		return instance;
	}

	/**
	 * 查询用户所在的组织机构列表，不包含子组织机构
	 * @see com.xteam.purview.service.IPurviewOrgFace#getOrgListByUserId(java.lang.String)
	 */
	public List<PurviewOrg> getOrgListByUserId(String userId) {
		String hql = IBaseDao.SQL_FROM + PurviewUserOrg.REF + IBaseDao.SQL_WHERE + PurviewUserOrg.PROP_USERID + "=?";
		try {
			Collection<String> orgId = new ArrayList<String>();
			// 查询用户的组织机构关系
			List<PurviewUserOrg> list = dao.queryAll(hql, new String[] { userId });
			for (PurviewUserOrg puo : list) {
				orgId.add(puo.getOrgId());
			}
			List<PurviewOrg> pos = null;
			if (orgId != null && orgId.size() > 0) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("orgId", orgId);
				String qhql = IBaseDao.SQL_FROM + PurviewOrg.REF + IBaseDao.SQL_WHERE + PurviewOrg.PROP_ORGID + " in (:orgId)";
				pos = dao.queryAll(qhql, param);
			}
			return pos;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 遍历用户所属的组织机构的所有子机构，并返回列表
	 * @author tangkf  ---2012-2-12
	 * @param userId
	 * @return
	 */
	public List<PurviewOrg> getAllOrgListByUserId(String userId) {
		List<PurviewOrg> olist	= this.getOrgListByUserId(userId);
		if(olist!=null){
			List<PurviewOrg> tpog	= new ArrayList<PurviewOrg>();
			for(PurviewOrg org:olist){
				List<PurviewOrg>  tmpall	= getAllChildrenOrg(org.getOrgId());
				if(olist!=null) tpog.addAll(tmpall);
			}
			if(tpog!=null && tpog.size()>0) olist.addAll(tpog);
		}
		return olist;
	}
	
	/**
	 * 递归遍历组织机构下的所有 子集 组织机构，深度遍历
	 * @author tangkf  ---2012-2-12
	 * @param orgId
	 * @return
	 */
	public List<PurviewOrg> getAllChildrenOrg(String orgId) {
		List<PurviewOrg>  rlist	= getChildrenOrg(orgId);
		if(rlist!=null){
			List<PurviewOrg> tpog	= new ArrayList<PurviewOrg>();
			for(PurviewOrg org:rlist){
				List<PurviewOrg>  tmp	= getAllChildrenOrg(org.getOrgId());
				if(tmp!=null) tpog.addAll(tmp);
			}
			if(tpog!=null && tpog.size()>0) rlist.addAll(tpog);
		}
		return rlist;
	}

	/**
	 * 查询组织机构及其下级组织机构列表，深度只包含一级
	 * @author tangkf ---2012-2-11
	 * @param userId
	 * @return
	 */
	public List<PurviewOrg> getChildrenOrg(String orgId) {
		String qhql = IBaseDao.SQL_FROM + PurviewOrg.REF + IBaseDao.SQL_WHERE + PurviewOrg.PROP_PORGID + " =?";
		List<PurviewOrg> pos = null;
		try {
			pos = dao.queryAll(qhql, new String[]{orgId});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pos;
	}

	/**
	 * 删除组织机构与用户的关系
	 * @author tangkf  ---2012-2-12
	 * @param orgId
	 * @param userId
	 * @return
	 */
	public int delOrgUser(String orgId, String[] userId) {
		int r = 0;
		String hql = IBaseDao.SQL_DELETE + IBaseDao.SQL_FROM + PurviewUserOrg.REF + IBaseDao.SQL_WHERE + PurviewUserOrg.PROP_ORGID + "=:orgId "
				+ IBaseDao.SQL_AND + PurviewUserOrg.PROP_USERID + " in(:userId)";
		if (orgId != null && userId != null && userId.length > 0) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("orgId", orgId);
			param.put("userId", userId);
			try {
				r = dao.executeHql(hql, param);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return r;
	}

	/**
	 * 返回组织机构中的用户列表
	 * @author tangkf  ---2012-2-12
	 * @param orgId
	 * @return
	 */
	public List<PurviewUser> getUserListByOrgId(String orgId) {
		String hql = IBaseDao.SQL_FROM + PurviewUserOrg.REF + IBaseDao.SQL_WHERE + PurviewUserOrg.PROP_ORGID + "=?";
		try {
			Collection<String> userId = new ArrayList<String>();
			List<PurviewUserOrg> list = dao.queryAll(hql, new String[] { orgId });
			for (PurviewUserOrg puo : list) {
				userId.add(puo.getUserId());
			}
			List<PurviewUser> pus = null;
			if (userId != null && userId.size() > 0) {
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("userId", userId);
				String qhql = IBaseDao.SQL_FROM + PurviewUser.REF + IBaseDao.SQL_WHERE + PurviewUser.PROP_USERID + " in (:userId)";
				pus = dao.queryAll(qhql, param);
			}
			return pus;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 保存用户组织机构关系
	 * @author tangkf ---2012-2-11
	 * @param orgId 组织机构ID
	 * @param userId 用户ID
	 * @return
	 */
	public int saveUserOrg(String orgId, String[] userId) {
		int r = 0;

		if (CommonUtils.notEmpty(orgId) && userId != null) {
			for (String uid : userId) {
				PurviewUserOrg puo = new PurviewUserOrg();
				puo.setOrgId(orgId);
				puo.setUserId(uid);
				try {
					String hql = IBaseDao.SQL_FROM + PurviewUserOrg.REF + IBaseDao.SQL_WHERE + PurviewUserOrg.PROP_ORGID + "=?" + IBaseDao.SQL_AND
							+ PurviewUserOrg.PROP_USERID + "=?";
					List<?> list = dao.queryAll(hql, new String[] { orgId, uid });
					if (CommonUtils.isEmpty(list)) {
						r = dao.saveObject(puo);
					}
				} catch (Exception e) {
					e.printStackTrace();
					r = 1;
				}
			}
		}
		return r;
	}
}